﻿using FastUtil.Base;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace FastUtil.Base
{
    public class OfficeUtil : BaseUtil
    {
        public static bool ConvertWordToPdf(string docFilePath, string pdfFilePath)
        {
            object paramMissing = Type.Missing;
            if (!File.Exists(docFilePath))
                return false;
            try
            {
                Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
                Document wordDocument = null;

                object paramSourceDocPath = docFilePath;
                string paramExportFilePath = pdfFilePath;

                WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
                bool paramOpenAfterExport = false;
                WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateHeadingBookmarks;//.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;

                try
                {
                    // 打开word文件
                    wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    // 输出到pdf格式
                    if (wordDocument != null)
                    {

                        wordApplication.ActiveDocument.Fields.Update();
                        wordDocument.ExportAsFixedFormat(paramExportFilePath,
                                                         paramExportFormat, paramOpenAfterExport,
                                                         paramExportOptimizeFor, paramExportRange, paramStartPage,
                                                         paramEndPage, paramExportItem, paramIncludeDocProps,
                                                         paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                                                         paramBitmapMissingFonts, paramUseISO19005_1,
                                                         ref paramMissing);
                        LogUtil.WriteLog("OfficeUtil_ConvertWordToPdf", "doc转pdf Service Info:doc文档转化pdf文档转化成功。");
                    }
                }
                //在服务器端记录错误信息
                catch (Exception ex)
                {
                    LogUtil.WriteLog("OfficeUtil_ConvertWordToPdf", "doc转pdf Service error: " + ex.Message);
                    return false;
                }
                finally
                {
                    //关闭文档实体
                    if (wordDocument != null)
                    {
                        wordDocument.Close(ref paramMissing, ref paramMissing,
                                           ref paramMissing);
                        wordDocument = null;
                    }
                    // 关闭word进程
                    if (wordApplication != null)
                    {
                        wordApplication.Quit(ref paramMissing, ref paramMissing,
                                             ref paramMissing);
                        wordApplication = null;
                    }
                    //垃圾回收
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
                return true;
            }
            //在服务器端记录错误信息
            catch (Exception err)
            {
                LogUtil.WriteLog("OfficeUtil_ConvertWordToPdf", "doc转pdf Service error: " + err.Message);
                return false;
            }
        }
    }
}
